#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | foam-extend: Open Source CFD
#  \\    /   O peration     | Version:     4.1
#   \\  /    A nd           | Web:         http://www.foam-extend.org
#    \\/     M anipulation  | For copyright notice see file Copyright
#------------------------------------------------------------------------------
# License
#     This file is part of foam-extend.
#
#     foam-extend is free software: you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by the
#     Free Software Foundation, either version 3 of the License, or (at your
#     option) any later version.
#
#     foam-extend is distributed in the hope that it will be useful, but
#     WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
#
# Script
#     wmakePrintBuild
#
# Description
#     Print the version used when building the project.
#
#------------------------------------------------------------------------------
Script=${0##*/}

usage() {
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE
usage: $Script [OPTION]
options:
  -check          check the git head commit vs. \$WM_PROJECT_DIR/.build
                  (exit code 0 for no changes)
  -major          report \$WM_PROJECT_VERSION only and exit
  -update         update \$WM_PROJECT_DIR/.build from the git information
  -version VER    specify an alternative version

Print the version used when building the project, in this order of precedence:
  * the git head commit (prefixed with \$WM_PROJECT_VERSION)
  * \$WM_PROJECT_DIR/.build
  * \$WM_PROJECT_VERSION

USAGE
    exit 1
}
#------------------------------------------------------------------------------

unset checkOnly update version

# parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    -h | -help)
        usage
        ;;
    -c | -check)
        checkOnly=true
        shift
        ;;
    -major)
        echo ${WM_PROJECT_VERSION:-unknown}
        exit 0
        ;;
    -u | -update)
        update=true
        shift
        ;;
    -v | -version)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        version=$2
        shift 2
        ;;
    *)
        usage "unknown option/argument: '$*'"
        ;;
    esac
done

#------------------------------------------------------------------------------

#
# persistent build tag
#
build="$WM_PROJECT_DIR/.build"
previous=$(tail -1 $build 2>/dev/null)

if [ -n "$version" ]
then
    # specified a version - no error possible
    rc=0
else
    # get the head SHA1 when building under git
    # if there are multiple values (eg, HEAD, origin/HEAD, ...)
    # only take the first one, which is 'HEAD'
    version=$(git show-ref --hash=12 --head HEAD 2>/dev/null | head -1)

    if [ -n "$version" ]
    then
        # mark as success and prefix with WM_PROJECT_VERSION
        rc=0
        version="${WM_PROJECT_VERSION}-$version"
    else
        # mark as failure
        rc=1
    fi
fi


# update persistent build tag if possible
if [ $rc -eq 0 -a -n "$update" -a "$version" != "$previous" ]
then
    if [ -w "$build" -o \( -w "$WM_PROJECT_DIR" -a ! -e "$build" \) ]
    then
        echo $version >| "$build" 2>/dev/null
    fi
fi


# check git vs. persistent build tag
if [ -n "$checkOnly" ]
then
    if [ $rc -eq 0 ]
    then
        test "$version" = "$previous"
        rc=$?
        if [ $rc -eq 0 ]
        then
            echo "same version as previous build"
        else
            echo "version changed from previous build"
        fi
    else
        echo "no git description found"
    fi
    exit $rc
fi


if [ $rc -eq 0 ]
then
    # output the git information or the -version version
    echo $version
elif [ -n "$previous" ]
then
    # use previous build tag
    echo $previous
else
    # fallback to WM_PROJECT_VERSION alone
    echo ${WM_PROJECT_VERSION:-unknown}
fi

#------------------------------------------------------------------------------
